<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isDone">
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="task"
      @keydown.enter="addFn"
    />
  </header>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
export default {
  data () {
    return {
      task:'',
    }
  },
  computed:{
    ...mapState(['list']),
    isDone:{
      set(val){
        this.allIsDone(val)
      },
      get () {
        return this.list.every(item=>item.isDone)
      }
    }
  },
  methods:{
    ...mapMutations(['setList','allIsDone']),
    addFn(){
      if(this.task.trim()===''){
        alert('输入不能为空')
        return
      }
      this.setList({
        id: this.list.length===0 ? 100:this.$store.state.list.at(-1).id ++,
        name:this.task,
        isDone:false
      })
      this.task = ''
    }
  },

}
</script>
